package com.uhealin;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Mapper.Context;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class MR2 {

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		// args0:dst
		// args1:out
		// args2:splitMB
		// args3:reduce num

		if (args.length != 4) {
			System.out.println("args0:dst");
			System.out.println("args1:out");
			System.out.println("args2:splitMB");
			System.out.println("args3:reduce num");
			System.exit(0);
		}
		int SplitMB = Integer.valueOf(args[2]);
		String dst = args[0];
		String out = args[1];
		Configuration conf = new Configuration();
		conf.set("mapreduce.input.fileinputformat.split.maxsize",
				String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapred.min.split.size", String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapreduce.input.fileinputformat.split.minsize.per.node",
				String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapreduce.input.fileinputformat.split.minsize.per.rack",
				String.valueOf(SplitMB * 1024 * 1024));
		conf.set("mapred.job.reuse.jvm.num.tasks", "-1");
		conf.set("mapred.child.java.opts", "-Xmx350m -Xmx1024m");
		// conf.set("fs.hdfs.impl",
		// org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
		// conf.set("fs.file.impl",
		// org.apache.hadoop.fs.LocalFileSystem.class.getName());
		Job job = Job.getInstance(conf);
		FileInputFormat.addInputPath(job, new Path(dst));
		FileOutputFormat.setOutputPath(job, new Path(out));
		job.setPartitionerClass(Partition.class);
		job.setGroupingComparatorClass(Group.class);
		// job.setSortComparatorClass(Group.class);
		job.setMapperClass(AppMap.class);
		job.setReducerClass(AppReduce.class);
		job.setNumReduceTasks(Integer.valueOf(args[3]));
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setOutputKeyClass(IntWritable.class);
		job.setJarByClass(MR2.class);
		job.setOutputValueClass(Text.class);
		// job.setCombinerClass(GetReduce.class);
		job.waitForCompletion(true);

	}
	
	public static class AppMap extends Mapper<Object,Text,Text,Text>{
		@Override
		public void map(Object key,Text value,Context context) throws IOException, InterruptedException{
			String[] line = value.toString().split("\t");
			if(line.length >= 3){
				context.write(new Text(line[0]),new Text(line[1]));
			}
		}
	}
	
	public static class Partition extends Partitioner<Text, Text> {

		@Override
		public int getPartition(Text arg0, Text arg1, int arg2) {
			String k = arg0.toString();
			int hash = k.hashCode();
			int result = Math.abs(hash % arg2);
			return result;
		}

	}
	
	public static class Group extends WritableComparator {
		protected Group() {
			super(Text.class, true);
		}

		@Override
		public int compare(WritableComparable w1, WritableComparable w2) {
			Text h1 = (Text)w1;
			Text h2 = (Text)w2;
			int R = -1;

			if (h1.equals(h2)) {
				R = 0;
			} else {
				R = h1.compareTo(h2);
			}

			if (R != 0) {
				R = R / Math.abs(R);
			}
			return R;
		}
	}
	
	public static class AppReduce extends
	Reducer<Text, Text, Text, Text> {
      @Override
      public void reduce(Text key, Iterable<Text> values, Context context)
		throws IOException, InterruptedException {
	   String jd = key.toString();
	   StringBuffer buffer = new StringBuffer();
	   for (Text t : values){
		   buffer.append("\t");
		   buffer.append(t.toString());
	      }
		context.write(new Text(jd), new Text(buffer.toString()));
}
}
}
